package com.danger.Activity.Fragment;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.danger.Activity.Bean.Course;
import com.danger.Activity.MainActivity;
import com.danger.Activity.R;
import com.danger.Activity.Util.ImageLoaderUtil;

/**
 * Created by 俊 on 2016/5/14.
 */
public class DetailFragment extends BackHandledFragment {

    private boolean hadIntercept;
    private View view;
    private CollapsingToolbarLayout toolbar_layout;
    private Toolbar toolbar;
    private TextView title_textView;
    private TextView content_textView;
    private MainActivity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_detail, container, false);
        }

        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        initView();

        return view;
    }

    private void initView() {
        activity = (MainActivity) getActivity();
        toolbar_layout = (CollapsingToolbarLayout) view.findViewById(R.id.toolbar_layout);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("详情");
        toolbar.setTitleTextColor(0X000000);
        activity.setSupportActionBar(toolbar);
        title_textView = (TextView) view.findViewById(R.id.title_textView);
        content_textView = (TextView) view.findViewById(R.id.content_textView);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        Bundle bundle = getArguments();
        title_textView.setText(((Course) (bundle.getSerializable("object"))).getName());
        content_textView.setText(((Course) (bundle.getSerializable("object"))).getDescription());
        new ImageLoaderUtil(toolbar_layout, ((Course) (bundle.getSerializable("object"))).getPicSmall(), activity).loadLayoutBackGround();
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }
}
